package cn.itcast.multiactivity;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText m_et;
    private Button m_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityResultLauncher<Intent> intentActivityResultLauncher =
                registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        //TODO:xxx
                        if(result.getData() != null && result.getResultCode() == Activity.RESULT_OK){
                            String backInfo = result.getData().getStringExtra("BackInfo");
                            Toast.makeText(MainActivity.this, backInfo, Toast.LENGTH_LONG).show();
                        }
                    }
                });
        m_et = (EditText) findViewById(R.id.et_no);
        m_btn = (Button) findViewById(R.id.btn_next);
        m_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_no = m_et.getText().toString();
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SecondActivity.class);
                intent.putExtra("No", str_no);
                intent.putExtra("School", "商务信息学院");
                //startActivity(intent);
                //startActivityForResult(intent, 1);
                intentActivityResultLauncher.launch(intent);
            }
        });
    }
}